package com.woniu.adopt.web;

import com.github.pagehelper.PageInfo;
import com.woniu.adopt.entity.Role;
import com.woniu.adopt.result.ResultResponse;
import com.woniu.adopt.service.RoleService;
import com.woniu.adopt.vo.RecycleVo;
import com.woniu.adopt.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("role")
@Api(description = "角色管理接口")
public class RoleController {

    @Autowired
    RoleService roleService;

    @ApiOperation(value = "分页显示所有角色功能")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "now", value = "查询的当前页", required = true, defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "size", value = "每页显示多少条", required = true, defaultValue = "6"),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "name", value = "角色名称", required = false)
    })
    @GetMapping("/list")
    public ResultResponse<PageInfo<Role>> rlist(@RequestParam(value = "now", defaultValue = "1") Integer now,
                                                @RequestParam(value = "size", defaultValue = "6") Integer size,
                                                String name) {
        ResultResponse<PageInfo<Role>> result = null;

        try {
            PageInfo<Role> rolePageInfo = roleService.queryByPage(now, size, name);
            result = new ResultResponse<>(200, "查询角色成功!", rolePageInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询角色失败!");
        }

        return result;
    }

    @ApiOperation(value = "修改角色功能")
    @PutMapping("/modify")
    public ResultResponse<Void> modify(@RequestBody Role role) {
        ResultResponse<Void> result = null;

        try {
            //根据 ID 查询出来 当前数据  把前台传递过来的数据  赋值到新查询出来的对象上
            Role r = roleService.queryRoleByKey(role.getId());
            r.setUpdateTime(new Date());
            r.setName(role.getName());
            roleService.modify(r);
            result = new ResultResponse<>(200, "修改角色成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改角色失败!");
        }
        return result;
    }

    @ApiOperation(value = "添加角色功能")
    @PostMapping("/add")
    public ResultResponse<Void> add(@RequestBody Role role) {
        ResultResponse<Void> result = null;

        try {
            role.setUpdateTime(new Date());
            role.setInsertTime(new Date());
            roleService.add(role);
            result = new ResultResponse<>(200, "添加角色成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "添加角色失败!");
        }
        return result;
    }

    @ApiOperation(value = "删除角色功能")
    @DeleteMapping("/del/{id}")
    public ResultResponse<Void> del(@PathVariable("id") long id) {
        ResultResponse<Void> result = null;

        try {
            roleService.removeBykey(id);
            result = new ResultResponse<>(200, "删除角色成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "删除角色失败!");
        }
        return result;
    }

    @ApiOperation(value = "保存角色的权限功能")
    @PutMapping("/authority")
    public ResultResponse<Void> authority(@RequestBody RoleVo roleVo) {
        ResultResponse<Void> result = null;
        try {
            roleService.authority(roleVo);
            result = new ResultResponse<>(200, "授权成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "授权失败!");
        }
        return result;
    }


    @ApiOperation(value = "显示所有角色功能")
    @GetMapping("/showAllRole")
    public ResultResponse<List<Role>> showAllRole() {
        ResultResponse<List<Role>> result = null;

        try {
            List<Role> list = roleService.queryAllRole();
            result = new ResultResponse<>(200, "查询角色成功!", list);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询角色失败!");
        }

        return result;
    }

    @ApiOperation(value = "显示所有回收角色列表功能")
    @GetMapping("/showAllRecycle")
    public ResultResponse<List<RecycleVo>> showAllRecycle() {
        ResultResponse<List<RecycleVo>> result = null;

        try {
            List<RecycleVo> list = roleService.selectRecycle();
            result = new ResultResponse<>(200, "查询系统已删信息成功!", list);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询失败!");
        }

        return result;
    }

    @ApiOperation(value = "角色回收功能")
    @PostMapping("/doRecycle")
    public ResultResponse<Void> doRecycle(@RequestParam("id") int id, @RequestParam("type") String type) {
        ResultResponse<Void> result = null;
        try {
            int i = roleService.doRecycle(id, type);
            result = new ResultResponse<>(200, "回收成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "回收失败!");
        }

        return result;
    }
}
